package com.example.controller;

import com.common.result.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import com.pojo.dto.QueryPageDTO;
import com.pojo.entity.SetmealDish;
import com.example.service.ISetmealDishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 套餐菜品关系 前端控制器
 * </p>
 *
 * @author ccz
 * @since 2025-07-19
 */
@RestController
@RequestMapping("/setmeal-dish")
@Slf4j
public class SetmealDishController {

    @Autowired
    private ISetmealDishService setmealDishService;

    /**
     * 新增套餐菜品关系
     */
    @PostMapping
    public Result save(@RequestBody SetmealDish setmealDish) {
        setmealDishService.save(setmealDish);
        return Result.success("新增套餐菜品关系成功");
    }

    /**
     * 批量新增套餐菜品关系
     */
    @PostMapping("/batch")
    public Result saveBatch(@RequestBody List<SetmealDish> setmealDishList) {
        setmealDishService.saveBatch(setmealDishList);
        return Result.success("批量新增套餐菜品关系成功");
    }

    /**
     * 分页查询套餐菜品关系
     */
    @GetMapping("/page")
    public Result page(QueryPageDTO queryPageDTO) {
        PageHelper.startPage(queryPageDTO.getPageNum(), queryPageDTO.getPageSize());
        List<SetmealDish> list = setmealDishService.list();
        PageInfo<SetmealDish> pageInfo = new PageInfo<>(list);
        return Result.success(pageInfo);
    }

    /**
     * 查询所有套餐菜品关系
     */
    @GetMapping
    public Result list() {
        List<SetmealDish> list = setmealDishService.list();
        return Result.success(list);
    }

    /**
     * 根据套餐ID查询菜品关系
     */
    @GetMapping("/setmeal/{setmealId}")
    public Result getBySetmealId(@PathVariable Long setmealId) {
        List<SetmealDish> list = setmealDishService.lambdaQuery()
                .eq(SetmealDish::getSetmealId, setmealId)
                .list();
        return Result.success(list);
    }

    /**
     * 根据菜品ID查询套餐关系
     */
    @GetMapping("/dish/{dishId}")
    public Result getByDishId(@PathVariable Long dishId) {
        List<SetmealDish> list = setmealDishService.lambdaQuery()
                .eq(SetmealDish::getDishId, dishId)
                .list();
        return Result.success(list);
    }

    /**
     * 根据ID查询套餐菜品关系
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id) {
        SetmealDish setmealDish = setmealDishService.getById(id);
        return Result.success(setmealDish);
    }

    /**
     * 修改套餐菜品关系
     */
    @PutMapping
    public Result update(@RequestBody SetmealDish setmealDish) {
        setmealDishService.updateById(setmealDish);
        return Result.success("修改套餐菜品关系成功");
    }

    /**
     * 删除套餐菜品关系
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        setmealDishService.removeById(id);
        return Result.success("删除套餐菜品关系成功");
    }

    /**
     * 根据套餐ID删除所有菜品关系
     */
    @DeleteMapping("/setmeal/{setmealId}")
    public Result deleteBySetmealId(@PathVariable Long setmealId) {
        setmealDishService.lambdaUpdate()
                .eq(SetmealDish::getSetmealId, setmealId)
                .remove();
        return Result.success("删除套餐菜品关系成功");
    }

    /**
     * 批量删除套餐菜品关系
     */
    @DeleteMapping("/batch")
    public Result deleteBatch(@RequestBody List<Long> ids) {
        setmealDishService.removeByIds(ids);
        return Result.success("批量删除套餐菜品关系成功");
    }
}
